package main

import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapreduce.Reducer

class CourseScoreAverageReducer: Reducer<Text, IntWritable, Text, IntWritable>() {
  override fun reduce(key: Text?, values: MutableIterable<IntWritable>?, context: Context?) {
    var sum = 0
    var count = 0
    values!!.forEach {
      sum += it.get()
      count++
    }
    val average = sum / count
    context!!.write(key, IntWritable(average))
    context.getCounter("reducer: ", "<${key.toString()}, ${average}>").increment(1)
  }
}